package me.youline.dataServer.service.pubMsg;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import me.youline.dataServer.service.support.file.PubMsgSubjectImgFileLocator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.base.Function;
import com.google.common.collect.Lists;

@Component
public class PubMsgSubjectService {

	@Autowired
	private PubMsgSubjectImgFileLocator fileLocator;

	/**
	 * 获取主题图片web url列表
	 * @return
	 */
	public List<String> loadSubjectImgUrls() {
		return Lists.transform(this.loadSubjectImgs(fileLocator.getFileRoot()),
				new Function<File, String>() {
					@Override
					public String apply(File input) {
						return getFileWebUrl(input.getName());
					}
				});
	}

	/**
	 * 获取主题相关图片文件
	 * @param folderUrl
	 * @return
	 */
	protected List<File> loadSubjectImgs(String folderUrl) {
		File[] files = new File(folderUrl).listFiles();
		if(files != null){
			return Arrays.asList(new File(folderUrl).listFiles());
		}
		return Collections.emptyList();
	}

	/**
	 * 获取文件对应的web url
	 * @param name
	 * @return
	 */
	protected String getFileWebUrl(String name) {
		return fileLocator.getFileWebUrl(name);
	}

	public PubMsgSubjectImgFileLocator getFileLocator() {
		return fileLocator;
	}

	public void setFileLocator(PubMsgSubjectImgFileLocator fileLocator) {
		this.fileLocator = fileLocator;
	}
	
}
